package com.cskaoyan.controller;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsGalleryList;
import com.cskaoyan.bo.UpdateGoodsBo;
import com.cskaoyan.service.GoodsServiceImpl;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.CatAndBrandVo;
import com.cskaoyan.vo.GoodsDetailVo;
import com.cskaoyan.vo.GoodsListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodsController {

    @Autowired
    GoodsServiceImpl goodsService;

    /**
     * 作者：grz
     * 功能：商品管理页面获取商品列表
     *
     * @param page  页数
     * @param limit 每页展示数量
     * @param order 正序倒序
     * @param sort  排序方式
     * @return BaseRespVo
     */
    @RequestMapping("admin/goods/list")
    @RequiresPermissions(value = "admin:goods:list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {

        GoodsListVo goodsListVo = goodsService.getGoodsList(page, limit, sort, order, goodsSn, name);

        return BaseRespVo.ok(goodsListVo);
    }
    /**
     * 作者：grz
     * 功能：商品管理页面删除商品
     *
     * @param goods 商品信息
     * @return BaseRespVo
     */
    @RequestMapping("/admin/goods/delete")
    @RequiresPermissions(value = "admin:goods:delete")
    public BaseRespVo delete(@RequestBody Goods goods) {

        int i = goodsService.delete(goods);

        if (i == 404) {
            return BaseRespVo.error("商品删除失败");
        }

        return BaseRespVo.ok();
    }

    /**
     * 作者：grz
     * 功能：新建商品界面获取分类与品牌
     *
     * @return BaseRespVo
     */
    @RequestMapping("/admin/goods/catAndBrand")
    public BaseRespVo catAndBrand() {

        CatAndBrandVo catAndBrandVo = goodsService.getCatAndBrand();

        return BaseRespVo.ok(catAndBrandVo);

    }

    /**
     * 作者：grz
     * 功能：获取商品详细信息
     *
     * @param id 商品的id
     * @return BaseRespVo
     */
    @RequestMapping("/admin/goods/detail")
    @RequiresPermissions(value = "admin:goods:detail")
    public BaseRespVo detail(Integer id) {

        GoodsDetailVo goodsDetailVo = goodsService.getDetail(id);

        return BaseRespVo.ok(goodsDetailVo);

    }

    /**
     * 作者：grz
     * 功能：商品更新
     *
     * @param goodsDetailVo 商品信息类
     * @return BaseRespVo
     */
    @RequestMapping("/admin/goods/update")
    public BaseRespVo update(@RequestBody GoodsDetailVo goodsDetailVo) {

        //参数验证，规格不允许为空
        if (goodsDetailVo.getSpecifications().size() == 0) {
            return BaseRespVo.error("更新失败，规格为空");
        }

        int i = goodsService.update(goodsDetailVo);

        if (i == 404) {
            return BaseRespVo.error("商品修改失败");
        }
        return BaseRespVo.ok();
    }


    /**
     * 作者：grz
     * 功能：上架商品
     */
    @RequestMapping("/admin/goods/create")
    @RequiresPermissions(value = "admin:goods:create")
    public BaseRespVo create(@RequestBody UpdateGoodsBo updateGoodsBo) {

        //参数验证
        //商品名称和商品SN不能为空
        GoodsGalleryList goods = updateGoodsBo.getGoods();
        if (goods.getGoodsSn() == null || goods.getName() == null) {
            return BaseRespVo.error("商品名称与商品编号必须填写");
        }

        int i = goodsService.create(updateGoodsBo);

        if (i == 404) {
            return BaseRespVo.error("商品添加失败");
        }
        return BaseRespVo.ok("商品添加成功");
    }
}